package com.ctrl.riveradministrator.mvp.view.activity.to_do_list;

import android.content.Context;
import android.graphics.Color;
import android.os.Build;
import android.os.Message;
import android.support.annotation.RequiresApi;
import android.support.v4.widget.SwipeRefreshLayout;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.cazaea.sweetalert.SweetAlertDialog;
import com.ctrl.riveradministrator.R;
import com.ctrl.riveradministrator.base.BaseActivity;
import com.ctrl.riveradministrator.base.BaseListAdapter;
import com.ctrl.riveradministrator.RaApplication;
import com.ctrl.riveradministrator.mvp.model.ToDo;
import com.ctrl.riveradministrator.mvp.presenter.ToDoPresenter;

import java.util.List;

import static com.baidu.mapapi.BMapManager.getContext;

public class ToDoActivity extends BaseActivity<ToDoPresenter> implements ToDoView,
        BaseListAdapter.OnItemClickListener<ToDo> {

    private Toolbar toolbar;
    private TextView titleTv;
    private ImageView leftMenu;
    private RecyclerView rl;
    private SwipeRefreshLayout sfl;
    private LinearLayoutManager layoutManager;
    private ToDoAdapter toDoAdapter;

    @Override
    protected void initParams(Bundle parms) {

    }

    @Override
    protected int bindLayout() {
        return R.layout.activity_to_do;
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void initView(View view) {
        toolbar = $(R.id.toolbar);
        titleTv = $(R.id.title_tv);
        leftMenu = $(R.id.left_iv);
        rl = $(R.id.todo_list);
        sfl = $(R.id.todo_srl);

        layoutManager = new LinearLayoutManager(getContext());
        rl.setLayoutManager(layoutManager);
        sfl.setColorSchemeColors(Color.RED, Color.BLUE);
        sfl.setEnabled(false);
        toDoAdapter = new ToDoAdapter(this);
        rl.setAdapter(toDoAdapter);

        pDialog.getProgressHelper().setBarColor(Color.parseColor("#A5DC86"));
        pDialog.setTitleText("加载中...");
        pDialog.setCancelable(false);

        /*
         * 设置toolbar
         * */
        toolbar.setTitleTextColor(Color.WHITE);
        toolbar.setTitle("");
        titleTv.setText(getString(R.string.to_do));
        leftMenu.setBackground(getDrawable(R.drawable.ic_chevron_left_24dp));
    }

    @Override
    protected void doBusiness(Context mContext) {
        mPresenter.getToDoList(RaApplication.getUserId());
    }

    @Override
    protected void widgetClick(View view) {
        switch (view.getId()) {
            case R.id.left_iv:
                finish();
                break;
        }
    }

    @Override
    protected void setListener() {
        leftMenu.setOnClickListener(this);
        toDoAdapter.setOnItemClickListener(this);
    }

    @Override
    public void handleMessage(Message message, int what) {

    }

    @Override
    protected ToDoPresenter createPresenter() {
        return new ToDoPresenter(this);
    }

    @Override
    public void showMessage(List<ToDo> toDos) {
        toDoAdapter.updateListView(toDos);
    }

    @Override
    public void onItemClick(ToDo toDo, int position) {
        SweetAlertDialog sweetAlertDialog = new SweetAlertDialog(this);
        sweetAlertDialog.setTitleText(toDo.getPatrolName());
        sweetAlertDialog.setContentText("问题描述：" + toDo.getProblemDescribe() + "\n");
        sweetAlertDialog.setCancelable(true);
        sweetAlertDialog.show();
    }

    @Override
    public void showLoading() {
        super.showLoading();
        //pDialog.show();
    }

    @Override
    public void hideLoading() {
        super.hideLoading();
        //pDialog.cancel();
    }
}
